<!-- 学校编辑弹窗 -->
<template>
  <el-dialog
    title="学校二维码"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      label-width="100px">
        
        <el-form-item
        label="学校名称:"
        prop="name">
        <el-input
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入学校名称"
          :disabled="true"/>
      </el-form-item>
      
      <el-form-item label="二维码:">
        <uploadImage :limit="1" v-model="form.qrcode"></uploadImage>
      </el-form-item>
                        
      
      
      </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="downloadEwm(form.id)"
        :loading="loading">下载
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'
import { saveAs } from 'file-saver';

export default {
  name: 'SchoolQrcode',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
        
      } else {
        this.form = {};
        this.isUpdate = false;
        
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
            // 城市数据处理
          this.form = Object.assign({}, this.form, {
            city: this.city
          });
          this.$http.post('/school/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },

    downloadCodeImg(imageUrl,name) {
        saveAs(imageUrl,name + '.png');
    },
    
    downloadImg(url, fileName) {
        const a = document.createElement('a');
        a.href = url;
        a.setAttribute('download',fileName + '.png');
        a.click();
        document.body.removeChild(a); // 下载完成后移除链接
    },
    downloadEwm(school_id){
         window.open(process.env.VUE_APP_API_BASE_URL + 'filedownload/image?school_id=' + school_id);
    }

  }
}
</script>

<style scoped>
</style>
